package A_review.review5_8;

import java.util.HashSet;

public class CanBuildLongestPalindrome {

        public int longestPalindrome(String s) {
            if (s.length() == 0)
            return 0;
// 用于存放字符
            HashSet<Character> hashset = new HashSet<Character>();
            char[] chars = s.toCharArray();
            int count = 0;
            //  CKG TODO 2023/5/8: 方法很绝　　　不统计个数就能知道
            for (int i = 0; i < chars.length; i++) {
                if (!hashset.contains(chars[i])) {// 如果hashset没有该字符就保存进去
                    hashset.add(chars[i]);
                } else {// 如果有,就让count++(说明找到了一个成对的字符),然后把该字符移除
                    hashset.remove(chars[i]);
                    //  CKG TODO 2023/5/8:  只有某个字符为偶数个的时候　ｃｏｕｎｔ才会＋＋
                    count++;
                }
            }
            //  CKG TODO 2023/5/8:hashset里面存储的都是奇数个数的元素　有的话就ｃｏｕｎｔ＊２＋１
            return hashset.isEmpty() ? count * 2 : count * 2 + 1;
        }


    public int longestPalindrome1(String s) {
            if (s.length()==0){
                return 0;
            }
        char[] chars = s.toCharArray();
        HashSet<Character> set = new HashSet<>();
        int count=0;
        for (int i = 0; i < chars.length ; i++) {
            if (!set.contains(chars[i])){
                set.add(chars[i]);
            }
            else {
                set.remove(chars[i]);
                count++;
            }
        }
        return   set.isEmpty()?count*2:count*2+1;
    }


    public static void main(String[] args) {
        CanBuildLongestPalindrome canBuildLongestPalindrome = new CanBuildLongestPalindrome();
        int abb = canBuildLongestPalindrome.longestPalindrome1("abbbb");
        System.out.println(abb);
    }






    }


